﻿@{
    ViewBag.Title = "字典信息维护";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-md-3">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">字典结构</h3>
                <div class="box-tools pull-right">
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                    <a class="btn btn-danger  btn-sm" data-widget="collapse" data-toggle="tooltip" title="伸缩" style="margin-right: 5px;"><i class="fa fa-minus"></i></a>
                </div>
            </div>
            <div class="box-body" id="tree-box-body" style="overflow: auto">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div class="col-md-9">

        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">字典列表</h3>
                <div class="box-tools">
                    <div class="input-group" style="width: 250px;">
                        <input type="text" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                        <div class="input-group-btn">
                            <a class="btn btn-sm btn-default"><i class="fa fa-search"></i></a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-body no-padding">
                <div class="mailbox-controls">
                    <div class="btn-group" id="partial_button_toolbar">
                        @{ Html.RenderPartial("~/Areas/Common/Views/Shared/_PartialButton.cshtml"); }
                    </div>
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                </div>
                <div class="mailbox-jqgrid">
                    <table id="list"></table>
                    <div id="pager"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        height = UtilWindowHeightWidth().WinH - 218;
        $("#tree-box-body").height(height);
        initGird();
        initTreeData();
    });

    var zNodes,
        treeObj,
        $grid,
        height;

    //初始化表格
    function initGird() {
        $grid = $("#list").jgridview(
        {
            shrinkToFit: true,
            multiselect: false,
            url: "/System/Dictionary/GetDictionariesByParentId",
            colModel: [
                { name: "DictionaryId", index: "DictionaryId", hidden: true },
                { label: "代码", name: "Code", width: 150, fixed: true },
                { label: "名称", name: "Name", width: 150, fixed: true },
                { label: "值", name: "Value", width: 200, fixed: true },
                { label: "允许删除", formatter: "YesOrNo", name: "CanbeDelete", width: 70, align: "center", fixed: true },
                { label: "排序", align: "center", name: "OrderNo", width: 50, fixed: true, search: false, sorttype: "int" },
                { label: "备注", name: "Remark", width: 150 }
            ],
            height: height - 100,
            rowNum: 200,
            postData: { id: Language.common.guidempty },
            rowList: [200, 500, 1000]
        });
    }

    //获取表格数据
    function getGridData() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        var pId = treeNode.length === 0 ? Language.common.guidempty : treeNode[0].id;
        UtilAjaxPost("/System/Dictionary/GetDictionariesByParentId", { id: pId }, function (data) {
            GridReloadLoadOnceData($grid, data);
        });
    }

    //初始化树
    function initTree() {
        //配置
        var setting = {
            view: {
                dblClickExpand: false,
                showLine: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: onClickTree
            }
        };
        treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
    }

    //初始化树结构
    function initTreeData() {
        UtilAjaxPost("/Dictionary/GetDictionaryTree", {}, function (data) {
            zNodes = data;
            initTree();
        });
    }

    //重新计算树高度宽度
    function reloadTreeSpace() {
        $("#tree").height($("#uiWest").height() - 38).width($("#uiWest").width() - 10);
    }

    //树点击触发
    function onClickTree(e, treeId, treeNode) {
        //加载数据
        getGridData();
    }

    var dicObj = null;
    //加载树和列表数据
    function loadTreeAndGrid() {
        initTreeData();
        var note = treeObj.getNodeByParam("id", dicObj.pId, null);
        //选中加载前节点
        treeObj.selectNode(note);
        treeObj.expandNode(note);
        getGridData();
    }

    //操作:新增
    function add() {
        var pid = Language.common.guidempty;
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        if (treeNode.length !== 0) {
            pid = treeNode[0].id;
        }
        ArtDialogOpen("/System/Dictionary/Edit?parentId=" + pid, "新增字典", 610);
    }

    //操作:编辑
    function edit() {
        //查看是否选中
        GridIsSelect($grid, function () {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Dictionary/Edit?dictionaryId=" + info.DictionaryId, "编辑字典-" + info.Name, 610);
        });
    }

    //删除匹配项
    function del() {
        //查看是否选中
        GridIsSelect($grid, function () {
            ArtDialogConfirm(Language.common.deletemsg, function () {
                UtilAjaxPostWait(
                    "/System/Dictionary/DeleteDictionary",
                    { id: GridGetSingSelectData($grid).DictionaryId },
                    perateStatus
                );
            });
        });
    }

    //请求完成
    function perateStatus(data) {
        DialogAjaxResult(data);
        if (data.ResultSign === 0) {
            //获取重新加载前选中树
            var breforeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
            initTreeData();
            //重新获取该选中树
            if (breforeNode.length !== 0) {
                //获取选中前节点
                var note = treeObj.getNodeByParam("id", breforeNode[0].id, null);
                //获取选中前节点
                treeObj.selectNode(note);
                treeObj.expandNode(note);
            }
            getGridData();
        }
    }

    //批量生成代码
    function generatingCode() {
        ArtDialogConfirm("是否批量生成代码?生成的代码将覆盖以前代码,使用字典代码处可能有误", function () {
            UtilAjaxPostWait(
                "/System/Dictionary/GeneratingCode",
                {},
                perateStatus
            );
        });
    }

    //重新加载
    function reload() {
        initTreeData();
        getGridData();
    }
</script>